package Test.FOURTH.four;

import java.io.*;
import java.net.Socket;

public class FileUploadClient {
    private final static String HOST="localhost";
    private final static int PORT=8001;

    private Socket socket;
    private OutputStream os;

    public FileUploadClient(){
        try {

            socket = new Socket(HOST, PORT);
            System.out.println("--客户端启动--");
            os=socket.getOutputStream();
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception{
        FileUploadClient fileUploadClient=new FileUploadClient();
        File file=new File("FileUploadClientText.txt");
        int c=0;
        char ch;

        if (file.exists()){
            FileInputStream fis=new FileInputStream(file);
            BufferedInputStream bis=new BufferedInputStream(fis);

            BufferedOutputStream bos=new BufferedOutputStream(fileUploadClient.os);

            while (c!=-1){
                c=bis.read();
                ch=(char)c;
                System.out.print(ch);
                bos.write(c);
            }

            bos.flush();
            System.out.println("文件上传成功！");
        }else{
            System.err.println("文件不存在，上传失败！");
        }
    }

}
